package com.example.mvvmdemo.ui.activity;

import android.content.Intent;
import android.view.View;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.mvvmdemo.R;
import com.example.common.base.BaseActivity;
import com.example.mvvmdemo.databinding.ActivityMainBinding;
import com.example.mvvmdemo.model.User;
import com.example.mvvmdemo.ui.adapter.UserAdapter;
import com.example.common.utils.EmptyDataHandler;
import com.example.common.utils.LogUtil;
import com.example.mvvmdemo.viewmodel.UserViewModel;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, UserViewModel> implements EmptyDataHandler {
    
    private UserAdapter userAdapter;
    
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
    
    @Override
    protected Class<UserViewModel> getViewModelClass() {
        return UserViewModel.class;
    }
    
    @Override
    protected void initView() {
        binding.setViewModel(viewModel);
        
        userAdapter = new UserAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(userAdapter);
        
        userAdapter.setOnUserClickListener(user -> {
            LogUtil.d("MainActivity", "User clicked: " + user.getName());
        });
        
        binding.btnRetry.setOnClickListener(v -> onRetryClick());
        
        // 个人中心按钮点击事件
        binding.btnProfile.setOnClickListener(v -> {
            LogUtil.d("MainActivity", "Profile button clicked");
            startActivity(new Intent(this, ProfileActivity.class));
        });
    }
    
    @Override
    protected void initData() {
        viewModel.loadUsers(true);
    }
    
    @Override
    protected void initObserver() {
        viewModel.userListData.observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                LogUtil.d("MainActivity", "Users received: " + users.size());
                userAdapter.setUserList(users);
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.tvEmpty.setVisibility(View.GONE);
                binding.tvError.setVisibility(View.GONE);
            }
        });
        
        viewModel.isLoading.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean isLoading) {
                binding.progressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
                if (isLoading) {
                    binding.recyclerView.setVisibility(View.GONE);
                    binding.tvEmpty.setVisibility(View.GONE);
                    binding.tvError.setVisibility(View.GONE);
                }
            }
        });
        
        viewModel.error.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String error) {
                if (error != null && !error.isEmpty()) {
                    binding.tvError.setText(error);
                    binding.tvError.setVisibility(View.VISIBLE);
                    binding.recyclerView.setVisibility(View.GONE);
                    binding.tvEmpty.setVisibility(View.GONE);
                }
            }
        });
        
        viewModel.isEmpty.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean isEmpty) {
                if (isEmpty != null && isEmpty) {
                    binding.tvEmpty.setVisibility(View.VISIBLE);
                    binding.recyclerView.setVisibility(View.GONE);
                    binding.tvError.setVisibility(View.GONE);
                }
            }
        });
    }
    
    @Override
    public void onRetryClick() {
        LogUtil.d("MainActivity", "Retry clicked");
        viewModel.retry();
    }
}